#include "bsp_cfg.h"

#include "uart.h"
#include "i2cmastermon.h"

void bsp_init(void)
{
	I2CMasterMon_Init();
	UARTIntInit();
}

void ddc_write(I2C_Device dev, unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
    I2C_master_rw_synchronous(dev, EDID_ADDR, &pageaddr, 1, I2C_MODE_SkipStop);
    I2C_master_rw_synchronous(dev, EDID_ADDR, pdata, length, I2C_MODE_SkipStart);
}
void ddc_read(I2C_Device dev, unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
    I2C_master_rw_synchronous(dev, EDID_ADDR, &pageaddr, 1, I2C_MODE_SkipStop);
    I2C_master_rw_synchronous(dev, EDID_ADDR+1, pdata, length, I2C_MODE_Normal);
}
void deva_ddc_write(unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
	ddc_write(dev_a,pageaddr,pdata,length);
}
void devb_ddc_write(unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
	ddc_write(dev_b,pageaddr,pdata,length);
}
void devc_ddc_write(unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
	ddc_write(dev_c,pageaddr,pdata,length);
}
void deva_ddc_read(unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
	ddc_read(dev_a,pageaddr,pdata,length);
}
void devb_ddc_read(unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
	ddc_read(dev_b,pageaddr,pdata,length);
}
void devc_ddc_read(unsigned char pageaddr, unsigned char *pdata, unsigned char length)
{
	ddc_read(dev_c,pageaddr,pdata,length);
}

static void uart_putbuf(unsigned char * buf,unsigned char length)
{
	unsigned char i = 0,chtemp;
	while(i!=length)
	{		
		ADD_QUEUE_DATA(UartTX, buf[i]);
		i++;
	}
	GET_QUEUE_DATA(UartTX, &chtemp);
	TXREG = chtemp;
}

void uart_loop(void)
{
	unsigned char chtemp;
	if(GET_QUEUE_COUNT(UartRX)){
		GET_QUEUE_DATA(UartRX, &chtemp);
		uart_putbuf(&chtemp,1);
    }
}

void interrupt Intr(void)
{
    UartISR();
}